package com.ruoyi.lvlian.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.lvlian.domain.chart.ChartData;
import com.ruoyi.lvlian.domain.chart.PlaneData;
import com.ruoyi.lvlian.dto.ChartDataDto;
import com.ruoyi.lvlian.dto.InvoiceInfoDto;
import com.ruoyi.lvlian.service.ICommonService;
import com.ruoyi.lvlian.service.IDashBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import javax.validation.Valid;
import java.util.*;

@Api(value = "图表统计", tags = "图表统计dashBoard")
@RestController
@RequestMapping("/dashBoard")
public class DashBoardController extends BaseController {
    @Autowired
    private IDashBoardService dashBoardService;
    @Autowired
    private ICommonService iCommonService;

    @ApiOperation("1-统计13%--开票金额趋势-13%发票-按出项方 category传13 ")
    @GetMapping(value = "/queryInvoiceInfoTotalAmountBySellerName")
    public AjaxResult queryInvoiceInfoTotalAmountBySellerName(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryInvoiceInfoTotalAmountBySellerName(dto);
        return success(chartData);
    }

    @ApiOperation("1-统计13%-开票数量趋势-13%发票-按出项方 category传13 ")
    @GetMapping(value = "/queryInvoiceInfoCntBySellerName")
    public AjaxResult queryInvoiceInfoCntBySellerName(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        return success(dashBoardService.queryInvoiceInfoCntBySellerName(dto));
    }


    @ApiOperation("1-统计13%13%开票数量总计-按出项方 ==环形图 category传13 ")
    @GetMapping(value = "/queryInvoiceInfoCntBySellerName2d")
    public AjaxResult queryInvoiceInfoCntBySellerName2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryInvoiceInfoCntBySellerName2D(dto);
        return success(chartData);
    }

    @ApiOperation("1-统计13%--销售金额总计-按出项方  ====玫瑰图 category传13 ")
    @GetMapping(value = "/queryInvoiceInfoTotalAmountBySellerName2d")
    public AjaxResult queryInvoiceInfoTotalAmountBySellerName2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryInvoiceInfoTotalAmountBySellerName2d(dto);
        return success(chartData);
    }


    @ApiOperation("3销售收款额趋势-按出项方 ==渐变堆叠面积图  category传参 采购付款信息/销售收款信息 ")
    @GetMapping(value = "/queryPaymentInfoAmountBySellerName")
    public AjaxResult queryPaymentInfoAmountBySellerName(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryPaymentInfoAmountBySellerName(dto);
        return success(chartData);
    }

    @ApiOperation("3销售收款额总计-按出项方 ==基础南丁格尔玫瑰图  category传参 采购付款信息/销售收款信息 ")
    @GetMapping(value = "/queryPaymentInfoAmountBySellerNameTotal")
    public AjaxResult queryPaymentInfoAmountBySellerNameTotal(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryPaymentInfoAmountBySellerNameTotal(dto);
        return success(chartData);
    }





    @ApiOperation("2销售物流---销售金额趋势-按出项方 传参   销售物流结算/采购物流结算")
    @GetMapping(value = "/queryWarehouseInfoSellerSettlementAmountBySellerName3d")
    public AjaxResult queryWarehouseInfoSellerSettlementAmountBySellerName3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryWarehouseInfoSellerSettlementAmountBySellerName3d(dto);
        return success(chartData);
    }


    @ApiOperation("2销售物流---销售金额总计-按出项方 传参   销售物流结算/采购物流结算")
    @GetMapping(value = "/queryWarehouseInfoSellerSettlementAmountBySellerName2d")
    public AjaxResult queryWarehouseInfoSellerSettlementAmountBySellerName2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryWarehouseInfoSellerSettlementAmountBySellerName2d(dto);
        return success(chartData);
    }


    @ApiOperation("2销售物流--销售订单数量趋势-按出项方入库单  传参   销售物流结算/采购物流结算")
    @GetMapping(value = "/queryWarehouseInfoSellerCnt3d")
    public AjaxResult queryWarehouseInfoSellerCnt3d(@Valid  ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryWarehouseInfoSellerCnt3d(dto);
        return success(chartData);
    }
    @ApiOperation("2销售物流-销售订单数量总计-按出项方 ==环形图 传参   销售物流结算/采购物流结算")
    @GetMapping(value = "/queryWarehouseInfoSellerCnt2d")
    public AjaxResult queryWarehouseInfoSellerCnt2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryWarehouseInfoSellerCnt2d(dto);
        return success(chartData);
    }


    @ApiOperation("3销售收款回单数量趋势-按出项方 堆积柱状图  category传参 采购付款信息/销售收款信息")
    @GetMapping(value = "/queryPaymentInfoCntBySellerName3d")
    public AjaxResult queryPaymentInfoCntBySellerName3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryPaymentInfoCntBySellerName3d(dto);
        return success(chartData);
    }

    @ApiOperation("3销售收款回单数量总计-按出项方 ==环形图  category传参 采购付款信息/销售收款信息")
    @GetMapping(value = "/queryPaymentInfoCntBySellerName")
    public AjaxResult queryPaymentInfoCntBySellerName(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryPaymentInfoCntBySellerName(dto);
        return success(chartData);
    }


    @ApiOperation("4重庆卫华-合同数据统计 阶梯折线图 传参   采购合同/供销合同")
    @GetMapping(value = "/queryContractInfoByContractType3d")
    public AjaxResult queryContractInfoByContractType3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryContractInfoByContractType3d(dto);
        return success(chartData);
    }




    @ApiOperation("4合同数量总计-按类型  传参   采购合同/供销合同")
    @GetMapping(value = "/queryContractInfoByContractType2d")
    public AjaxResult queryContractInfoByContractType2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryContractInfoByContractType2d(dto);
        return success(chartData);
    }



    @ApiOperation("5车辆数量总计-按出项方 ==环形图    传参   销售物流结算/采购物流结算")
    @GetMapping(value = "/queryVehicleInfoCntBySellerName2d")
    public AjaxResult queryVehicleInfoCntBySellerName2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryVehicleInfoCntBySellerName2d(dto);
        return success(chartData);
    }


    @ApiOperation("2销售物流-- 复用5--  车辆数量趋势-按出项方 ==堆叠   传参   销售物流结算/采购物流结算  ")
    @GetMapping(value = "/queryVehicleInfoCntBySellerName3d")
    public AjaxResult queryVehicleInfoCntBySellerName3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryVehicleInfoCntBySellerName3d(dto);
        return success(chartData);
    }





    @ApiOperation("7供应商数量趋势-按类型 传参  供应商/利废商 ")
    @GetMapping(value = "/queryBusinessLicenseInfoByType3d")
    public AjaxResult queryBusinessLicenseInfoByType3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryBusinessLicenseInfoByType3d(dto);
        return success(chartData);
    }
    @ApiOperation("7供应商数量趋势-按类型 传参  供应商/利废商 ")
    @GetMapping(value = "/queryBusinessLicenseInfoByType2d")
    public AjaxResult queryBusinessLicenseInfoByType2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryBusinessLicenseInfoByType2d(dto);
        return success(chartData);
    }



    @ApiOperation("6司机 2复用---车辆数量总计-按出项方  传参   销售物流结算/采购物流结算  ")
    @GetMapping(value = "/queryDriverInfoCnt2d")
    public AjaxResult queryDriverInfoCnt2d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        List<PlaneData> chartData = dashBoardService.queryDriverInfoCnt2d(dto);
        return success(chartData);
    }


    @ApiOperation("6司机--车辆数量趋势-按出项方 传参   销售物流结算/采购物流结算  ")
    @GetMapping(value = "/queryDriverInfoCnt3d")
    public AjaxResult queryDriverInfoCnt3d(@Valid ChartDataDto dto) {
        List<String>   orgCodes =generalCodes( dto.getOrgCodes());
        if (orgCodes.size() == 0) {
            return AjaxResult.error("暂未分配账本");
        }
        if(dto.getOrgCodes()==null ){
            dto.setOrgCodes( orgCodes);
        }
        ChartData chartData = dashBoardService.queryDriverInfoCnt3d(dto);
        return success(chartData);
    }




    private List<String> generalCodes (List<String> orgCodes) {
        if (SecurityUtils.getUserId() == 1) {
            orgCodes = new ArrayList<>(Arrays.asList("50000000"));
        } else {
            if (orgCodes == null || orgCodes.isEmpty()) {
                orgCodes = iCommonService.queryCurrentOrgCode(SecurityUtils.getUserId());
                orgCodes.removeIf(code -> code.length() != 8);
            }
        }
        return orgCodes;
    }
}
